<?php
class ymdian_orderControl extends skymvc{
	private $admin;
	public function __construct(){
		parent::__construct();		
	}
	
	public function onInit(){
		$this->loadConfig("table");
		$this->admin=M("login")->getYmdianAdmin();
	}
	
	public function onNewJs(){
		$where=" shopid=".SHOPID." AND status =0 ";
		$row=M("ymdian_order")->selectRow($where);
		if($row){
			exit(json_encode(array("success"=>1)));
		}else{
			exit(json_encode(array("success"=>0)));
		}
	}
	
	public function onDefault(){
		$where=" shopid=".SHOPID." AND status in(0,1,2,3,10) ";
		$url=APPYMDIAN."?m=ymdian_order";
		$start=get('per_page','i');
		$limit=60;
		
		$orderno=get('orderno','h');
		if($orderno){
			$where.=" AND orderno='".$orderno."' ";
			$url.="&orderno=$orderno";
		}
		
		$nickname=get('ninckname','h');
		if($nickname){
			$user=M("user")->selectRow(array("where"=>"nickname='".$nickname."' "));
			if($user){
				$where.=" userid=".$user['userid'];
			}else{
				$where.=" 1=2 ";			
			}
			$url.="&nickname=".urlencode($nickname);
		}
		 
		$status=get('status','i');
		if($status>=0){
			$where.=" AND status=$status";
			$url.="&status=$status";
		}
		if(!isset($_GET['s_ispay'])){
			$ispay=-1;
		}else{
			$ispay=get('s_ispay','i');
		}
		if($ispay>=0){
			$where.=" AND ispay=$ispay";
			$url.="&s_ispay=$ispay"; 
		}
		$start_time=get('start_time','h');
		$end_time=get('end_time','h');
		if($start_time){
			$where.=" AND dateline>".strtotime($start_time)." ";
			$url.="&start_time=".$start_time;
		}
		
		if($end_time){
			$where.=" AND dateline<".strtotime($end_time)." ";
			$url.="&end_time=".$end_time;
		}
		$type_id=get_post('type_id','i');
		if($type_id){
			$where.=" AND type_id=".$type_id." ";
			$url.="&type_id=".$type_id;
		}
		$baseurl=$url;
		$index='';
		switch(get('index')){
			case 'asc':
					$index='asc';
				break;
			case 'desc':
					$index='desc';
				break;
		}
		switch(get('orderby')){
			case "id":
				$order=" order_id $index";
				break;
			case "sushe":
				$order=" sushe $index";
				break;
			default:
				$order=" order_id DESC";
				break;
		}
		$option=array(
			"where"=>$where,
			"order"=>$order,
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("ymdian_order")->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("ymdian_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"order"=>"id DESC"));
				$v['num']=M("ymdian_order_product")->getOne("select sum(amount) as ct from ".table('ymdian_order_product')." where order_id=".$v['order_id']." ");
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"baseurl"=>$baseurl,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("ymdian_order/index.html");
	}
	/*
	*订单详情
	*/
	public function onShow(){
		$order_id=get('order_id','i');
		$data=M("ymdian_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		$addr=M("ymdian_order_address")->selectRow(array("where"=>"order_id=".$order_id,"order"=>"id DESC"));
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		//获取商品
		$order_product=M("ymdian_order_product")->select(array("where"=>"order_id=".$data['order_id']));
		$total_num=0;
		foreach($order_product as $k=>$v){
			$p=M('product')->selectRow(array("where"=>"id=".$v['productid']));
			$ks=M('product_ks')->selectRow("id=".$v['ksid']);
			if($ks){
				$p['ks_title']=$ks['title'];
				$p['price']=$ks['price'];
			}
			$p['order_price']=$v['price'];
	 
			$p['amount']=$v['amount'];
			$total_num+=$v['amount'];
			$order_product[$k]=$p;
		}
		$data['total_num']=$total_num;
		$this->smarty->assign(array(
			"data"=>$data,
			"addr"=>$addr,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			"order_product"=>$order_product,
			"admin"=>$this->admin,
			"pay_type_list"=>pay_type_list(),
			"shop"=>M("ymdian")->selectRow("id=".$data['shopid']) 
		));
		$this->smarty->display("ymdian_order/show.html");
		
	}
	/**
	*订单确认
	*/
	public function onConfirm(){
		$order_id=get_post('order_id','i');
		$data=M("ymdian_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if(!$data['unpay'] && $data['ispay']!=2){
			$this->goAll("还未支付，不能确认订单");
		}
		if($data['shopid']!=SHOPID){
			$this->goAll("暂无权限");
		}
		M("ymdian_order")->update(array("status"=>1),"order_id=".$order_id);
		$content=post('content');
		M("ymdian_order_log")->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content,
		));
		
		//如果超出确认时间
		if($data['dateline']+YMDIAN_WG_QR<time()){
			M("ymdian_weigui")->insert(array(
				"k"=>"confirm",
				"shopid"=>$data['shopid'],
				"order_id"=>$data['order_id'],
				"dateline"=>time(),
				content=>"订单".$data['orderno']."超过规定的确认时间了",
			));
		}
		
		$msg=array(
			"dateline"=>time(),
			"type_id"=>1,
			"status"=>0,
			"userid"=>$data['userid'],
			"content"=>"订单<a href='/index.php?m=ymdian_order&a=show&order_id=".$data['order_id']."' target='_blank'>".$data['orderno']."</a>已确认",
		);
		$msg['id']=M("notice")->insert($msg);
		$this->goall("确认成功");
	}
	/**
	*订单发送
	*/
	public function onSend(){
		$order_id=get_post('order_id','i');
		$data=M("ymdian_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if(!$data['unpay'] && $data['ispay']!=2){
			$this->goAll("还未支付，不能发送订单");
		}
		if($data['shopid']!=SHOPID){
			$this->goAll("暂无权限");
		}
		$content=post('content');
		$shipping_info=post('shipping_info','h');
		$kdy_telephone=post('kdy_telephone','h');
		if($data['status']<2){
			M("ymdian_order")->update(array("status"=>2),"order_id=".$order_id);
		}
		
		M("ymdian_order_log")->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content."快递信息：".$shipping_info,
		));
		M("ymdian_order")->update(array("shipping_info"=>$shipping_info,"kdy_telephone"=>$kdy_telephone),"order_id=".$order_id);
		//如果超出确认时间
		if($data['dateline']+YMDIAN_WG_PS<time()){
			M("ymdian_weigui")->insert(array(
				"k"=>"confirm",
				"shopid"=>$data['shopid'],
				"order_id"=>$data['order_id'],
				"dateline"=>time(),
				content=>"订单".$data['orderno']."超过规定的确认时间了",
			));
		}
		
 
		$content="您的夜猫店订单<a href='/index.php?m=ymdian_order&a=show&order_id=".$data['order_id']."' target='_blank'>".$data['orderno']."</a>已发货";
		M("notice")->add(array(
				"content"=>$content,
				"userid"=>$data['userid'],
		));
		$this->goall("发货成功");
	}

	/**
	*订单完成
	*/
	public function onFinish(){
		
		$order_id=get_post('order_id','i');
		$data=M("ymdian_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data) or $data['status']>=3) $this->goall("参数出错",1);
		if(!$data['paytype']=='unpay' && $data['ispay']!=2){
			$this->goAll("还未支付，不能完成订单");
		}
		if($data['shopid']!=SHOPID){
			$this->goAll("暂无权限");
		}
		M("ymdian_order")->begin();
		M("ymdian_order")->update(array("status"=>3),"order_id=".$order_id);
		$content=post('content');
		M("ymdian_order_log")->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content,
		));
		//添加产品销售数量
		$order_product=M("ymdian_order_product")->select(array("where"=>"order_id=".$data['order_id']));
		foreach($order_product as $k=>$v){
			$product=M("product")->selectRow("id=".$v['productid']);
			M("ymdian_product")->changenum("buy_num",$v['amount'],"id=".$v['object_id']);
			//增加统计数量
			$option=array(
				"siteid"=>$data['siteid'],
				"shopid"=>$data['shopid'],
				"k"=>"ymdian",
				"productid"=>$v['productid'],
				"ordernum"=>$v['amount']
			);
			M("ymdian_product")->statDayAdd($option);
			M("ymdian_product")->statMonthAdd($option);
			M("ymdian_product")->statWeekAdd($option);
			
			M("product")->changenum("buy_num",$v['amount'],"id=".$v['productid']);
			if($v['ksid']){
				M('product_ks')->changenum("buy_num",$v['amount'],"id=".$v['ksid']);
			}
			if($product['zuhe_ids']){
				$zhids=explode(",",$product['zuhe_ids']);
				if(!empty($zhids)){
					foreach($zhids as $zz){
						
						if($zz<=0) continue;
						$option['productid']=$zz;
						M("ymdian_product")->statDayAdd($option);
						M("ymdian_product")->statMonthAdd($option);
						M("ymdian_product")->statWeekAdd($option);
						$product=M("product")->selectRow("id=".$zz);
						M("ymdian_product")->changenum("buy_num",$v['amount']," shopid=".$data['shopid']." AND productid=".$zz);
						M("product")->changenum("buy_num",$v['amount'],"id=".$zz);
					}
				}
			}
		}
		//处理收入分配
		$ymdian=M("ymdian")->selectRow("id=".$data['shopid']);
		$tgy=M("tgyuan_area")->selectRow("sc_id=".$ymdian['sc_id']);
		if($tgy){
			$tgyuan=M("tgyuan")->selectRow("id=".$tgy['tgyid']);
		}
		$money1=$data['money'];
		$money1_b=$money1*YM_GL_MONEY;
		$money2=$data['money'];
		$money2_b=$money2*YM_XC_MONEY;
		$on_money1=$on_money2=$off_money1=$off_money2=0;
		if($data['ispay']==2){
			$on_money1=$money1;
			$on_money2=$money2;
		}else{
			$off_money1=$money1;
			$off_money2=$money2;
		}
		//夜猫店统计
			M("ymdian")->addMoney(array(
					"shopid"=>$data['shopid'],
					"balance"=>$money1_b,
					"income"=>$money1,
					"content"=>"您的店铺订单".$data['orderno']."完成了，收入[money]元"
				));
			
			//添加今日收入
			$kday=date("Ymd");
			$rd=M("stat_shoporder_day")->selectRow(" shopid=".$data['shopid']." AND k='ymdian' AND kday='".$kday."' ");
			if($rd){
				M("stat_shoporder_day")->update(array(
					"money"=>$money1+$rd['money'],
					"ordernum"=>$rd['ordernum']+1,
					"balance"=>$money1_b+$rd['balance'],
					"on_money"=>$on_money1+$rd['on_money'],
					"off_money"=>$off_money1+$rd['off_money'],
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_day")->insert(array(
					"money"=>$money1,
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>1,
					"kday"=>$kday,
					"k"=>"ymdian",
					"shopid"=>$data['shopid'],
					"on_money"=>$on_money1,
					"off_money"=>$off_money1,
					"siteid"=>$data['siteid'],
				));
			}
			//添加本周收入
			$kweek=date("YW");
			$rd=M("stat_shoporder_week")->selectRow(" shopid=".$data['shopid']." AND k='ymdian' AND kweek='".$kweek."' ");
			if($rd){
				M("stat_shoporder_week")->update(array(
					"money"=>$money1+$rd['money'],
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>$rd['ordernum']+1,
					"on_money"=>$on_money1+$rd['on_money'],
					"off_money"=>$off_money1+$rd['off_money'],
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_week")->insert(array(
					"money"=>$money1,
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>1,
					"kweek"=>$kweek,
					"k"=>"ymdian",
					"shopid"=>$data['shopid'],
					"on_money"=>$on_money1,
					"off_money"=>$off_money1,
					"siteid"=>$data['siteid'],
				));
			}
			
			//添加本月收入
			$kmonth=date("Ym");
			$rd=M("stat_shoporder_month")->selectRow(" shopid=".$data['shopid']." AND k='ymdian' AND kmonth='".$kmonth."' ");
			if($rd){
				M("stat_shoporder_month")->update(array(
					"money"=>$money1+$rd['money'],
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>$rd['ordernum']+1,
					"on_money"=>$on_money1+$rd['on_money'],
					"off_money"=>$off_money1+$rd['off_money'],
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_month")->insert(array(
					"money"=>$money1,
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>1,
					"kmonth"=>$kmonth,
					"k"=>"ymdian",
					"shopid"=>$data['shopid'],
					"on_money"=>$on_money1,
					"off_money"=>$off_money1,
					"siteid"=>$data['siteid'],
				));
			}
			
		//ENd 夜猫店统计
		/***------------
		//宣传员统计
		if($tgyuan){	
			M('tgyuan')->changenum('income',$money2,"id=".$tgyuan['id']);
			$ntgyuan=M("tgyuan")->selectRow("id=".$tgyuan['id']);
			$logdata=array(
				"dateline"=>time(),
				"shopid"=>$tgyuan['id'],
				"type_id"=>1,
				"k"=>"income",
				"ispay"=>1,
				"money"=>$money1,
				"content"=>"店铺订单".$data['orderno']."完成了，收入".$money2."元,原来".$tgyuan['income'].",现在".$ntgyuan['income']."",
			);
			M('tgyuan_pay_log')->insert($logdata);
			//添加今日收入
			$kday=date("Ymd");
			$rd=M("stat_shoporder_day")->selectRow(" shopid=".$tgyuan['id']." AND k='tgyuan' AND kday='".$kday."' ");
			if($rd){
				M("stat_shoporder_day")->update(array(
					"money"=>$money2+$rd['money'],
					"ordernum"=>$rd['ordernum']+1,
					"balance"=>$money2_b+$rd['balance']
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_day")->insert(array(
					"money"=>$money2,
					"balance"=>$money2_b+$rd['balance'],
					"ordernum"=>1,
					"kday"=>$kday,
					"k"=>"tgyuan",
					"shopid"=>$tgyuan['id']
				));
			}
			//添加本周收入
			$kweek=date("YW");
			$rd=M("stat_shoporder_week")->selectRow(" shopid=".$tgyuan['id']." AND k='tgyuan' AND kweek='".$kweek."' ");
			if($rd){
				M("stat_shoporder_week")->update(array(
					"money"=>$money2+$rd['money'],
					"balance"=>$money2_b+$rd['balance'],
					"ordernum"=>$rd['ordernum']+1
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_week")->insert(array(
					"money"=>$money2,
					"balance"=>$money2_b+$rd['balance'],
					"ordernum"=>1,
					"kweek"=>$kweek,
					"k"=>"tgyuan",
					"shopid"=>$tgyuan['id']
				));
			}
			
			//添加本月收入
			$kmonth=date("Ym");
			$rd=M("stat_shoporder_month")->selectRow(" shopid=".$tgyuan['id']." AND k='tgyuan' AND kmonth='".$kmonth."' ");
			if($rd){
				M("stat_shoporder_month")->update(array(
					"money"=>$money2+$rd['money'],
					"balance"=>$money2_b+$rd['balance'],
					"ordernum"=>$rd['ordernum']+1
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_month")->insert(array(
					"money"=>$money2,
					"balance"=>$money2_b+$rd['balance'],
					"ordernum"=>1,
					"kmonth"=>$kmonth,
					"k"=>"tgyuan",
					"shopid"=>$tgyuan['id']
				));
			}
		}
		//ENd 宣传员统计	
		*****/	
		 
		
		//添加积分
		$this->loadControl("jfapi","source/index/");
		$this->jfapiControl->setUserId($data['userid'])->addGrade(array(
					"grade"=>$data['money'],
					"type_id"=>2,
					"content"=>"您的订单完成获得了".$data['money']."积分，之前有[oldgrade]分，目前有[newgrade]分",
		));
		$msg=array(
			"dateline"=>time(),
			"type_id"=>1,
			"status"=>0,
			"userid"=>$data['userid'],
			"content"=>"订单<a href='/index.php?m=ymdian_order&a=show&order_id=".$data['order_id']."' target='_blank'>".$data['orderno']."</a>已完成",
		);
		$msg['id']=M("notice")->insert($msg);
		M("ymdian_order")->commit();
		$this->goall("订单完成");
	}
	
	/**
	*取消订单
	*/
	 	
	public function onCancel(){
		
		$order_id=get_post('order_id','i');
		$data=M("ymdian_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['status']>=3 ) $this->goall("操作失败",1);
		if($data['shopid']!=SHOPID){
			$this->goAll("暂无权限");
		}
		M("ymdian_order")->begin();
		M("ymdian_order")->update(array("status"=>10,"whocancel"=>"ymdianadmin"),"order_id=".$order_id);
		$content=post('content');
		$message=post('message','h');
		M("ymdian_order_log")->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content."，原因：".$message,
		));
		//发送通知

	 	$content=$content."，原因：".$message;
		M("notice")->add(array(
				"content"=>$content,
				"userid"=>$data['userid'],
		));
		//退钱给用户
		if($data['ispay']==2){
			/**
			$row=M("recharge_back")->selectRow(" order_id=".$order_id." AND tablename='ymdian' ");
			if(empty($row)){
				M("recharge_back")->insert(array(
					"order_id"=>$order_id,
					"tablename"=>"ymdian",
					"paytype"=>$data['paytype'],
					"dateline"=>time(),
					"ip"=>ip(),
					"admin_id"=>$this->admin['id']
				));
			}
			**/
			M("user")->addMoney(array(
					"userid"=>$data['userid'],
					"money"=>$data['money'],
					"content"=>"您的订单被取消了返还[money]元",
			));
			
		}
		
		//加产品
		$order_product=M("ymdian_order_product")->select(array("where"=>"order_id=".$data['order_id']));
		foreach($order_product as $k=>$v){
			M("ymdian_product")->total_num_add(array(
					"shopid"=>$data['shopid'],
					"productid"=>$v['productid'],
					"total_num"=>$v['amount'],
					"content"=>"[订单取消]"
			));
		}
		M("ymdian_order")->commit();
		$this->goall("取消成功");
	}

	 
	
}
?>